<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>nginx相关 | 欢迎来到小羽的博客</title>
    <meta name="generator" content="VuePress 1.5.0">
    <link rel="icon" href="/favicon.ico">
    <script>
      var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?b61fe05f9bcd9cda05e839091f9a55a4";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();
    </script>
    <script type="text/javascript" src="/push.js"></script>
    <script type="text/javascript" src="/bdseo.js"></script>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    <link rel="preload" href="/assets/css/0.styles.535c5086.css" as="style"><link rel="preload" href="/assets/js/app.14e3b6f2.js" as="script"><link rel="preload" href="/assets/js/3.38ef4e4c.js" as="script"><link rel="preload" href="/assets/js/1.dd4aade8.js" as="script"><link rel="preload" href="/assets/js/32.e0da40e8.js" as="script"><link rel="prefetch" href="/assets/js/10.7327de6d.js"><link rel="prefetch" href="/assets/js/11.dbb0142d.js"><link rel="prefetch" href="/assets/js/12.b0df0a98.js"><link rel="prefetch" href="/assets/js/13.478ebb6f.js"><link rel="prefetch" href="/assets/js/14.5b94084a.js"><link rel="prefetch" href="/assets/js/15.159d1a32.js"><link rel="prefetch" href="/assets/js/16.cf37c681.js"><link rel="prefetch" href="/assets/js/17.61f89c90.js"><link rel="prefetch" href="/assets/js/18.52aae943.js"><link rel="prefetch" href="/assets/js/19.152fd37e.js"><link rel="prefetch" href="/assets/js/20.ce91f28e.js"><link rel="prefetch" href="/assets/js/21.47bc5b36.js"><link rel="prefetch" href="/assets/js/22.4bb2e24d.js"><link rel="prefetch" href="/assets/js/23.8156f657.js"><link rel="prefetch" href="/assets/js/24.867f6f46.js"><link rel="prefetch" href="/assets/js/25.1195288f.js"><link rel="prefetch" href="/assets/js/26.87eb9ee8.js"><link rel="prefetch" href="/assets/js/27.f67176ce.js"><link rel="prefetch" href="/assets/js/28.9574af6a.js"><link rel="prefetch" href="/assets/js/29.ce6e5d4c.js"><link rel="prefetch" href="/assets/js/30.5a16f9ff.js"><link rel="prefetch" href="/assets/js/31.d9981107.js"><link rel="prefetch" href="/assets/js/33.f50cc0ef.js"><link rel="prefetch" href="/assets/js/34.6d343a0b.js"><link rel="prefetch" href="/assets/js/35.6e3a0c31.js"><link rel="prefetch" href="/assets/js/36.38b9af99.js"><link rel="prefetch" href="/assets/js/37.87aede0d.js"><link rel="prefetch" href="/assets/js/4.6cf77dfa.js"><link rel="prefetch" href="/assets/js/5.c81d3913.js"><link rel="prefetch" href="/assets/js/6.8fadf7af.js"><link rel="prefetch" href="/assets/js/7.bb84db9a.js"><link rel="prefetch" href="/assets/js/8.d9edba17.js"><link rel="prefetch" href="/assets/js/9.c228f55c.js">
    <link rel="stylesheet" href="/assets/css/0.styles.535c5086.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-dad8a512><div data-v-dad8a512><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-dad8a512 data-v-dad8a512><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-64685f0e data-v-dad8a512 data-v-dad8a512><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>欢迎来到小羽的博客</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>小羽</span>
            
          <!---->
          2020
        </a></span></div></div> <div class="hide" data-v-dad8a512><header class="navbar" data-v-dad8a512><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="欢迎来到小羽的博客" class="logo"> <span class="site-name">欢迎来到小羽的博客</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/安全/" class="nav-link"><i class="iconfont undefined"></i>
  安全
</a></li><li class="dropdown-item"><!----> <a href="/categories/网络/" class="nav-link"><i class="iconfont undefined"></i>
  网络
</a></li><li class="dropdown-item"><!----> <a href="/categories/运维/" class="nav-link"><i class="iconfont undefined"></i>
  运维
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      其他
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/lyff1006" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/xiaoyu-web" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/fly821760648" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-csdn"></i>
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://juejin.im/user/3597257778926973" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-juejin"></i>
  掘金
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://www.jianshu.com/u/4ab50cbafc3f" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-jianshu"></i>
  简书
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-dad8a512></div> <aside class="sidebar" data-v-dad8a512><div class="personal-info-wrapper" data-v-ca798c94 data-v-dad8a512><img src="/logo.png" alt="author-avatar" class="personal-img" data-v-ca798c94> <h3 class="name" data-v-ca798c94>
    小羽
  </h3> <div class="num" data-v-ca798c94><div data-v-ca798c94><h3 data-v-ca798c94>22</h3> <h6 data-v-ca798c94>文章</h6></div> <div data-v-ca798c94><h3 data-v-ca798c94>35</h3> <h6 data-v-ca798c94>标签</h6></div></div> <hr data-v-ca798c94></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/安全/" class="nav-link"><i class="iconfont undefined"></i>
  安全
</a></li><li class="dropdown-item"><!----> <a href="/categories/网络/" class="nav-link"><i class="iconfont undefined"></i>
  网络
</a></li><li class="dropdown-item"><!----> <a href="/categories/运维/" class="nav-link"><i class="iconfont undefined"></i>
  运维
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      其他
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/lyff1006" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/xiaoyu-web" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/fly821760648" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-csdn"></i>
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://juejin.im/user/3597257778926973" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-juejin"></i>
  掘金
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://www.jianshu.com/u/4ab50cbafc3f" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-jianshu"></i>
  简书
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-64685f0e data-v-dad8a512><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>nginx相关</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>小羽</span>
            
          <!---->
          2020
        </a></span></div></div> <div data-v-dad8a512><main class="page"><div class="page-title" style="display:none;"><h1>nginx相关</h1> <hr> <div data-v-aa64b9d0><i class="iconfont reco-account" data-v-aa64b9d0><span data-v-aa64b9d0>小羽</span></i> <i class="iconfont reco-date" data-v-aa64b9d0><span data-v-aa64b9d0>2020-08-03</span></i> <!----> <i class="iconfont reco-tag tags" data-v-aa64b9d0><span class="tag-item" data-v-aa64b9d0>nginx</span><span class="tag-item" data-v-aa64b9d0>性能优化</span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><h1 id="_1-nginx相关命令"><a href="#_1-nginx相关命令" class="header-anchor">#</a> 1.nginx相关命令</h1> <blockquote><div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#启动</span>
start nginx

<span class="token comment">#快速关闭</span>
nginx -s stop

<span class="token comment">#优雅关闭</span>
nginx -s quit

<span class="token comment">#重启</span>
nginx -s reload

<span class="token comment">#查看版本</span>
nginx -V
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div></blockquote> <h1 id="_2-nginx调优"><a href="#_2-nginx调优" class="header-anchor">#</a> 2.nginx调优</h1> <h2 id="_2-1-工作进程数调优"><a href="#_2-1-工作进程数调优" class="header-anchor">#</a> 2.1 工作进程数调优</h2> <p>nginx运行工作进程个数一般设置为cpu的核心数或者线程数。如下则设置为4个进程</p> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token keyword">worker_processes</span> <span class="token number">4</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="_2-2cpu亲和力调优"><a href="#_2-2cpu亲和力调优" class="header-anchor">#</a> 2.2cpu亲和力调优</h2> <p>比如4核配置：</p> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token keyword">worker_processes</span> <span class="token number">4</span><span class="token punctuation">;</span>
<span class="token keyword">worker_cpu_affinity</span> <span class="token number">0001</span> <span class="token number">0010</span> <span class="token number">0100</span> <span class="token number">1000</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>比如8核配置：</p> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token keyword">worker_processes</span> <span class="token number">8</span><span class="token punctuation">;</span>
<span class="token keyword">worker_cpu_affinity</span> <span class="token number">00000001</span> <span class="token number">00000010</span> <span class="token number">00000100</span> <span class="token number">00001000</span> <span class="token number">00010000</span> <span class="token number">00100000</span> <span class="token number">01000000</span> <span class="token number">10000000</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="_2-3-最大打开文件数"><a href="#_2-3-最大打开文件数" class="header-anchor">#</a> 2.3 最大打开文件数</h2> <p>这个指令是指当一个nginx进程打开的最多文件描述符数目，理论值应该是最多打开文件数（ulimit -n）与nginx进程数相除，但是nginx分配请求并不是那么均匀，所以最好与ulimit -n的值保持一致。</p> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token keyword">worker_rlimit_nofile</span> <span class="token number">65535</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="_2-4-事件处理模型调优"><a href="#_2-4-事件处理模型调优" class="header-anchor">#</a> 2.4 事件处理模型调优</h2> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token keyword">events</span> <span class="token punctuation">{</span>
  <span class="token keyword">use</span> epoll<span class="token punctuation">;</span>
  <span class="token keyword">worker_connections</span> <span class="token number">65535</span><span class="token punctuation">;</span>
  <span class="token keyword">multi_accept</span> on<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>nginx采用epoll事件模型，处理效率高。</p> <p>work_connections是单个worker进程允许客户端最大连接数，这个数值一般根据服务器性能和内存来制定，实际最大值就是worker进程数乘以work_connections。</p> <p>实际我们填入一个65535，足够了，这些都算并发值，一个网站的并发达到这么大的数量，也算一个大站了！</p> <p>multi_accept 告诉nginx收到一个新连接通知后接受尽可能多的连接，默认是on，设置为on后，多个worker按串行方式来处理连接，也就是一个连接只有一个worker被唤醒，其他的处于休眠状态，设置为off后，多个worker按并行方式来处理连接，也就是一个连接会唤醒所有的worker，直到连接分配完毕，没有取得连接的继续休眠。当你的服务器连接数不多时，开启这个参数会让负载有一定的降低，但是当服务器的吞吐量很大时，为了效率，可以关闭这个参数。</p> <h2 id="_2-5-开启高效传输模式"><a href="#_2-5-开启高效传输模式" class="header-anchor">#</a> 2.5 开启高效传输模式</h2> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token keyword">http</span> <span class="token punctuation">{</span>
  <span class="token keyword">include</span> mime<span class="token punctuation">.</span><span class="token keyword">types</span><span class="token punctuation">;</span>
  <span class="token keyword">default_type</span> application<span class="token operator">/</span>octet<span class="token operator">-</span>stream<span class="token punctuation">;</span>
  ……

  <span class="token keyword">sendfile</span> on<span class="token punctuation">;</span>
  <span class="token keyword">tcp_nopush</span> on<span class="token punctuation">;</span>
  ……
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><ul><li>Include mime.types ： 媒体类型,include 只是一个在当前文件中包含另一个文件内容的指令。</li> <li>default_type application/octet-stream ：默认媒体类型足够。</li> <li>sendfile on：开启高效文件传输模式，sendfile指令指定nginx是否调用sendfile函数来输出文件，对于普通应用设为 on，如果用来进行下载等应用磁盘IO重负载应用，可设置为off，以平衡磁盘与网络I/O处理速度，降低系统的负载。注意：如果图片显示不正常把这个改成off。</li> <li>tcp_nopush on：必须在sendfile开启模式才有效，防止网路阻塞，积极的减少网络报文段的数量（将响应头和正文的开始部分一起发送，而不一个接一个的发送。）</li></ul> <h2 id="_2-6-连接超时时间"><a href="#_2-6-连接超时时间" class="header-anchor">#</a> 2.6 连接超时时间</h2> <p>主要目的是保护服务器资源，CPU，内存，控制连接数，因为建立连接也是需要消耗资源的。</p> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token keyword">keepalive_timeout</span> <span class="token number">60</span><span class="token punctuation">;</span>
<span class="token keyword">tcp_nodelay</span> on<span class="token punctuation">;</span>
<span class="token keyword">client_header_buffer_size</span> <span class="token number">4</span>k<span class="token punctuation">;</span>
<span class="token keyword">open_file_cache</span> max<span class="token operator">=</span><span class="token number">102400</span> inactive<span class="token operator">=</span><span class="token number">20</span>s<span class="token punctuation">;</span>
<span class="token keyword">open_file_cache_valid</span> <span class="token number">30</span>s<span class="token punctuation">;</span>
<span class="token keyword">open_file_cache_min_uses</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token keyword">client_header_timeout</span> <span class="token number">15</span><span class="token punctuation">;</span>
<span class="token keyword">client_body_timeout</span> <span class="token number">15</span><span class="token punctuation">;</span>
<span class="token keyword">reset_timedout_connection</span> on<span class="token punctuation">;</span>
<span class="token keyword">send_timeout</span> <span class="token number">15</span><span class="token punctuation">;</span>
<span class="token keyword">server_tokens</span> off<span class="token punctuation">;</span>
<span class="token keyword">client_max_body_size</span> <span class="token number">10</span>m<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><ul><li>keepalived_timeout ：客户端连接保持会话超时时间，超过这个时间，服务器断开这个链接。</li> <li>tcp_nodelay：也是防止网络阻塞，不过要包涵在keepalived参数才有效。</li> <li>client_header_buffer_size 4k：客户端请求头部的缓冲区大小，这个可以根据你的系统分页大小来设置，一般一个请求头的大小不会超过 1k，不过由于一般系统分页都要大于1k，所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。</li> <li>open_file_cache max=102400 inactive=20s ：这个将为打开文件指定缓存，默认是没有启用的，max指定缓存数量，建议和打开文件数一致，inactive 是指经过多长时间文件没被请求后删除缓存。</li> <li>open_file_cache_valid 30s：这个是指多长时间检查一次缓存的有效信息。</li> <li>open_file_cache_min_uses 1 ：open_file_cache指令中的inactive 参数时间内文件的最少使用次数，如果超过这个数字，文件描述符一直是在缓存中打开的，如上例，如果有一个文件在inactive 时间内一次没被使用，它将被移除。</li> <li>client_header_timeout ： 设置请求头的超时时间。我们也可以把这个设置低些，如果超过这个时间没有发送任何数据，nginx将返回request time out的错误。</li> <li>client_body_timeout设置请求体的超时时间。我们也可以把这个设置低些，超过这个时间没有发送任何数据，和上面一样的错误提示。</li> <li>reset_timeout_connection ：告诉nginx关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间。</li> <li>send_timeout ：响应客户端超时时间，这个超时时间仅限于两个活动之间的时间，如果超过这个时间，客户端没有任何活动，nginx关闭连接。</li> <li>server_tokens ：并不会让nginx执行的速度更快，但它可以关闭在错误页面中的nginx版本数字，这样对于安全性是有好处的。</li> <li>client_max_body_size：上传文件大小限制</li></ul> <h2 id="_2-7-fastcgi-调优"><a href="#_2-7-fastcgi-调优" class="header-anchor">#</a> 2.7 fastcgi 调优</h2> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token keyword">fastcgi_connect_timeout</span> <span class="token number">600</span><span class="token punctuation">;</span>
<span class="token keyword">fastcgi_send_timeout</span> <span class="token number">600</span><span class="token punctuation">;</span>
<span class="token keyword">fastcgi_read_timeout</span> <span class="token number">600</span><span class="token punctuation">;</span>
<span class="token keyword">fastcgi_buffer_size</span> <span class="token number">64</span>k<span class="token punctuation">;</span>
<span class="token keyword">fastcgi_buffers</span> <span class="token number">4</span> <span class="token number">64</span>k<span class="token punctuation">;</span>
<span class="token keyword">fastcgi_busy_buffers_size</span> <span class="token number">128</span>k<span class="token punctuation">;</span>
<span class="token keyword">fastcgi_temp_file_write_size</span> <span class="token number">128</span>k<span class="token punctuation">;</span>
<span class="token keyword">fastcgi_temp_path</span><span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>nginx1<span class="token punctuation">.</span><span class="token number">10</span><span class="token operator">/</span>nginx_tmp<span class="token punctuation">;</span>
<span class="token keyword">fastcgi_intercept_errors</span> on<span class="token punctuation">;</span>
<span class="token keyword">fastcgi_cache_path</span><span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>nginx1<span class="token punctuation">.</span><span class="token number">10</span><span class="token operator">/</span><span class="token keyword">fastcgi_cache</span> levels<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token number">2</span> keys_zone<span class="token operator">=</span>cache_fastcgi<span class="token punctuation">:</span><span class="token number">128</span>minactive<span class="token operator">=</span><span class="token number">1</span>d max_size<span class="token operator">=</span><span class="token number">10</span>g<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><ul><li>fastcgi_connect_timeout 600 ：指定连接到后端FastCGI的超时时间。</li> <li>fastcgi_send_timeout 600 ：向FastCGI传送请求的超时时间。</li> <li>fastcgi_read_timeout 600 ：指定接收FastCGI应答的超时时间。</li> <li>fastcgi_buffer_size 64k ：指定读取FastCGI应答第一部分需要用多大的缓冲区，默认的缓冲区大小为。fastcgi_buffers指令中的每块大小，可以将这个值设置更小。</li> <li>fastcgi_buffers 4 64k ：指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答请求，如果一个php脚本所产生的页面大小为256KB，那么会分配4个64KB的缓冲区来缓存，如果页面大小大于256KB，那么大于256KB的部分会缓存到fastcgi_temp_path指定的路径中，但是这并不是好方法，因为内存中的数据处理速度要快于磁盘。一般这个值应该为站点中php脚本所产生的页面大小的中间值，如果站点大部分脚本所产生的页面大小为256KB，那么可以把这个值设置为“8 32K”、“4 64k”等。</li> <li>fastcgi_busy_buffers_size 128k ：建议设置为fastcgi_buffers的两倍，繁忙时候的buffer。</li> <li>fastcgi_temp_file_write_size 128k ：在写入fastcgi_temp_path时将用多大的数据块，默认值是fastcgi_buffers的两倍，该数值设置小时若负载上来时可能报502BadGateway。</li> <li>fastcgi_temp_path  ：缓存临时目录。</li> <li>fastcgi_intercept_errors on ：这个指令指定是否传递4xx和5xx错误信息到客户端，或者允许nginx使用error_page处理错误信息。注：静态文件不存在会返回404页面，但是php页面则返回空白页！</li> <li>fastcgi_cache_path /usr/local/nginx1.10/fastcgi_cachelevels=1:2 keys_zone=cache_fastcgi:128minactive=1d max_size=10g ：fastcgi_cache缓存目录，可以设置目录层级，比如1:2会生成16*256个子目录，cache_fastcgi是这个缓存空间的名字，cache是用多少内存（这样热门的内容nginx直接放内存，提高访问速度），inactive表示默认失效时间，如果缓存数据在失效时间内没有被访问,将被删除，max_size表示最多用多少硬盘空间。</li> <li>fastcgi_cache cache_fastcgi ：#表示开启FastCGI缓存并为其指定一个名称。开启缓存非常有用，可以有效降低CPU的负载，并且防止502的错误放生。cache_fastcgi为proxy_cache_path指令创建的缓存区名称。</li> <li>fastcgi_cache_valid 200 302 1h ：#用来指定应答代码的缓存时间，实例中的值表示将200和302应答缓存一小时，要和fastcgi_cache配合使用。</li> <li>fastcgi_cache_valid 301 1d ：将301应答缓存一天。</li> <li>fastcgi_cache_valid any 1m ：将其他应答缓存为1分钟。</li> <li>fastcgi_cache_min_uses 1 ：该指令用于设置经过多少次请求的相同URL将被缓存。</li> <li>fastcgi_cache_key http://$host$request_uri ：该指令用来设置web缓存的Key值,nginx根据Key值md5哈希存储.一般根据$host(域名)、$request_uri(请求的路径)等变量组合成proxy_cache_key 。</li> <li>fastcgi_pass  ：指定FastCGI服务器监听端口与地址，可以是本机或者其它。</li></ul> <p><strong>总结：</strong></p> <p>nginx的缓存功能有：proxy_cache / fastcgi_cache</p> <ul><li>proxy_cache的作用是缓存后端服务器的内容，可能是任何内容，包括静态的和动态。</li> <li>fastcgi_cache的作用是缓存fastcgi生成的内容，很多情况是php生成的动态的内容。</li> <li>proxy_cache缓存减少了nginx与后端通信的次数，节省了传输时间和后端宽带。</li> <li>fastcgi_cache缓存减少了nginx与php的通信的次数，更减轻了php和数据库(mysql)的压力。</li></ul> <h2 id="_2-8-压缩调优"><a href="#_2-8-压缩调优" class="header-anchor">#</a> 2.8 压缩调优</h2> <h3 id="_2-8-1-gizp调优"><a href="#_2-8-1-gizp调优" class="header-anchor">#</a> 2.8.1 gizp调优</h3> <p>使用gzip压缩功能，可能为我们节约带宽，加快传输速度，有更好的体验，也为我们节约成本，所以说这是一个重点。</p> <p>Nginx启用压缩功能需要你来ngx_http_gzip_module模块，apache使用的是mod_deflate。</p> <p>一般我们需要压缩的内容有：文本，js，html，css，对于图片，视频，flash什么的不压缩，同时也要注意，我们使用gzip的功能是需要消耗CPU的！</p> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token keyword">gzip</span> on<span class="token punctuation">;</span>
<span class="token keyword">gzip_min_length</span> <span class="token number">2</span>k<span class="token punctuation">;</span>
<span class="token keyword">gzip_buffers</span>   <span class="token number">4</span> <span class="token number">32</span>k<span class="token punctuation">;</span>
<span class="token keyword">gzip_http_version</span> <span class="token number">1.1</span><span class="token punctuation">;</span>
<span class="token keyword">gzip_comp_level</span> <span class="token number">6</span><span class="token punctuation">;</span>
<span class="token keyword">gzip_types</span> text<span class="token operator">/</span>plain text<span class="token operator">/</span>css text<span class="token operator">/</span>javascript application<span class="token operator">/</span>json application<span class="token operator">/</span>javascript application<span class="token operator">/</span>x<span class="token operator">-</span>javascript application<span class="token operator">/</span>xml<span class="token punctuation">;</span>
<span class="token keyword">gzip_vary</span> on<span class="token punctuation">;</span>
<span class="token keyword">gzip_proxied</span> any<span class="token punctuation">;</span>
<span class="token keyword">gzip</span> on<span class="token punctuation">;</span>   <span class="token comment">#开启压缩功能</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><ul><li>gzip_min_length 1k ：设置允许压缩的页面最小字节数，页面字节数从header头的Content-Length中获取，默认值是0，不管页面多大都进行压缩，建议设置成大于1K，如果小与1K可能会越压越大。</li> <li>gzip_buffers 4 32k ：压缩缓冲区大小，表示申请4个单位为32K的内存作为压缩结果流缓存，默认值是申请与原始数据大小相同的内存空间来存储gzip压缩结果。</li> <li>gzip_http_version 1.1 ：压缩版本，用于设置识别HTTP协议版本，默认是1.1，目前大部分浏览器已经支持GZIP解压，使用默认即可。</li> <li>gzip_comp_level 6 ：压缩比例，用来指定GZIP压缩比，1压缩比最小，处理速度最快，9压缩比最大，传输速度快，但是处理慢，也比较消耗CPU资源。</li> <li>gzip_types text/css text/xml application/javascript ：用来指定压缩的类型，‘text/html’类型总是会被压缩。默认值: gzip_types text/html (默认不对js/css文件进行压缩)</li> <li><ul><li>压缩类型，匹配MIME型进行压缩；</li> <li>不能用通配符 text/*；</li> <li>text/html默认已经压缩 (无论是否指定)；</li> <li>设置哪压缩种文本文件可参考 conf/mime.types。</li></ul></li> <li>gzip_vary on ：varyheader支持，改选项可以让前端的缓存服务器缓存经过GZIP压缩的页面，例如用Squid缓存经过nginx压缩的数据。</li></ul> <h3 id="_2-8-2-brotli调优"><a href="#_2-8-2-brotli调优" class="header-anchor">#</a> 2.8.2 brotli调优</h3> <p><code>Brotli</code>是<code>Google</code>推出的开源<a href="https://blog.kieng.cn/tag/%E5%8E%8B%E7%BC%A9" target="_blank" rel="noopener noreferrer">压缩<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>算法，通过变种的<code>LZ77</code>算法、<code>Huffman</code>编码以及二阶文本建模等方式进行数据<a href="https://blog.kieng.cn/tag/%E5%8E%8B%E7%BC%A9" target="_blank" rel="noopener noreferrer">压缩<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>，与其他<a href="https://blog.kieng.cn/tag/%E5%8E%8B%E7%BC%A9" target="_blank" rel="noopener noreferrer">压缩<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>算法相比，它有着更高的<a href="https://blog.kieng.cn/tag/%E5%8E%8B%E7%BC%A9" target="_blank" rel="noopener noreferrer">压缩<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>效率，性能也比我们目前常见的<code>Gzip</code>高 17-25%，可以帮我们更高效的<a href="https://blog.kieng.cn/tag/%E5%8E%8B%E7%BC%A9" target="_blank" rel="noopener noreferrer">压缩<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>网页中的各类文件大小及脚本，从而提高加载速度，提升网页浏览体验。</p> <p>下载brotli模块</p> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token comment">#创建nginx第三方模块的路径</span>
mkdir <span class="token operator">/</span>home<span class="token operator">/</span>www<span class="token operator">/</span>nginx_extend
cd <span class="token operator">/</span>home<span class="token operator">/</span>www<span class="token operator">/</span>nginx_extend

<span class="token comment">#下载 brotli</span>
git clone <span class="token keyword">https</span><span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>github<span class="token punctuation">.</span>com<span class="token operator">/</span>google<span class="token operator">/</span>ngx_brotli<span class="token punctuation">.</span>git
cd ngx_brotli

<span class="token comment">#更新 brotli</span>
git submodule update <span class="token operator">--</span>init
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>查看nginx 版本为 1.14.0，configure arguments:后面的为你 nginx 的编译参数，下面会用到。</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token punctuation">[</span>root@VM_0_10_centos ~<span class="token punctuation">]</span><span class="token comment"># nginx -V</span>
nginx version: nginx/1.14.0
built by gcc <span class="token number">4.8</span>.5 <span class="token number">20150623</span> <span class="token punctuation">(</span>Red Hat <span class="token number">4.8</span>.5-39<span class="token punctuation">)</span> <span class="token punctuation">(</span>GCC<span class="token punctuation">)</span> 
built with OpenSSL <span class="token number">1.0</span>.2o  <span class="token number">27</span> Mar <span class="token number">2018</span>
TLS SNI support enabled
configure arguments: --user<span class="token operator">=</span>www --group<span class="token operator">=</span>www --prefix<span class="token operator">=</span>/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-openssl<span class="token operator">=</span>/root/lnmp1.5/src/openssl-1.0.2o
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>然后重新下载 nginx，并编译</p> <div class="language-SHELL line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#下载 nginx，这里下载的 1.14.0 版本，如果是其它版本，把下载链接的 1.14.0 改成你的版本号即可</span>
<span class="token function">wget</span> http://nginx.org/download/nginx-1.14.0.tar.gz

<span class="token comment">#解压并删除</span>
<span class="token function">tar</span> -xvzf nginx-*.tar.gz <span class="token operator">&amp;&amp;</span> <span class="token function">rm</span> -rf nginx-*.tar.gz

<span class="token comment">#进入 nginx 目录</span>
<span class="token builtin class-name">cd</span> nginx*

<span class="token comment">#生成 Makefile，./configure 后面的参数直接复制上面看到的，然后在后面额外加一个--add-module=/www/server/ngx_brotli</span>
./configure --user<span class="token operator">=</span>www --group<span class="token operator">=</span>www --prefix<span class="token operator">=</span>/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-openssl<span class="token operator">=</span>/root/lnmp1.5/src/openssl-1.0.2o

<span class="token comment">#编译 nginx</span>
<span class="token function">make</span>

<span class="token comment">#复制新编译完成的nginx文件到nginx目录下</span>
<span class="token function">cp</span> ./objs/nginx /usr/local/nginx/sbin

<span class="token comment">#重启nginx</span>
nginx -s reload

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p>编辑nginx.conf文件，使nginx开启brotli压缩</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">vim</span> /usr/local/nginx/conf/nginx.conf
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token comment">#br压缩</span>
brotli on<span class="token punctuation">;</span>              <span class="token comment">#启用</span>
brotli_comp_level <span class="token number">6</span><span class="token punctuation">;</span>    <span class="token comment">#等级，默认 6，最高 11，太高的压缩水平可能需要更多的 CPU</span>
brotli_buffers <span class="token number">16</span> <span class="token number">32</span>k<span class="token punctuation">;</span>   <span class="token comment">#请求缓冲区的数量和大小</span>
brotli_min_length <span class="token number">128</span><span class="token punctuation">;</span>   <span class="token comment">#指定压缩数据的最小长度，只有大于或等于最小长度才会对其压缩。这里指定 128 字节</span>
brotli_types text<span class="token operator">/</span>plain application<span class="token operator">/</span>javascript application<span class="token operator">/</span>x<span class="token operator">-</span>javascript text<span class="token operator">/</span>javascript text<span class="token operator">/</span>css application<span class="token operator">/</span>xml application<span class="token operator">/</span>json image<span class="token operator">/</span>svg application<span class="token operator">/</span>font<span class="token operator">-</span>woff application<span class="token operator">/</span>vnd<span class="token punctuation">.</span>ms<span class="token operator">-</span>fontobject application<span class="token operator">/</span>vnd<span class="token punctuation">.</span>apple<span class="token punctuation">.</span>mpegurl <span class="token punctuation">;</span>   <span class="token comment">#指定允许进行压缩类型</span>
brotli_static always<span class="token punctuation">;</span>   <span class="token comment">#是否允许查找预处理好的、以.br 结尾的压缩文件，可选值为 on、off、always</span>
brotli_window <span class="token number">512</span>k<span class="token punctuation">;</span>     <span class="token comment">#窗口值，默认值为 512k</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h2 id="_2-9-缓存调优"><a href="#_2-9-缓存调优" class="header-anchor">#</a> 2.9 缓存调优</h2> <p>缓存，主要针对于图片，css，js等元素更改机会比较少的情况下使用，特别是图片，占用带宽大，我们完全可以设置图片在浏览器本地缓存365d，css，js，html可以缓存个10来天，这样用户第一次打开加载慢一点，第二次，就非常快了！</p> <h3 id="_2-9-1-expires"><a href="#_2-9-1-expires" class="header-anchor">#</a> 2.9.1 expires</h3> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token keyword">location</span> <span class="token operator">~</span><span class="token operator">*</span> \<span class="token punctuation">.</span><span class="token punctuation">(</span>ico<span class="token operator">|</span>jpe<span class="token operator">?</span>g<span class="token operator">|</span>gif<span class="token operator">|</span>png<span class="token operator">|</span>bmp<span class="token operator">|</span>swf<span class="token operator">|</span><span class="token keyword">flv</span><span class="token punctuation">)</span>$ <span class="token punctuation">{</span>
    <span class="token keyword">expires</span> <span class="token number">30</span>d<span class="token punctuation">;</span>
    <span class="token comment">#log_not_found off;</span>
    <span class="token keyword">access_log</span> off<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">location</span> <span class="token operator">~</span><span class="token operator">*</span> \<span class="token punctuation">.</span><span class="token punctuation">(</span>js<span class="token operator">|</span>css<span class="token punctuation">)</span>$ <span class="token punctuation">{</span>
    <span class="token keyword">expires</span> <span class="token number">7</span>d<span class="token punctuation">;</span>
    <span class="token keyword">log_not_found</span> off<span class="token punctuation">;</span>
    <span class="token keyword">access_log</span> off<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>注：log_not_found off;是否在error_log中记录不存在的错误。默认是。</p> <h3 id="_2-9-2-cache-control"><a href="#_2-9-2-cache-control" class="header-anchor">#</a> 2.9.2 cache-control</h3> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code>locaktion <span class="token operator">~</span><span class="token operator">*</span> \<span class="token punctuation">.</span><span class="token punctuation">(</span>ico<span class="token operator">|</span>jpe<span class="token operator">?</span>g<span class="token operator">|</span>gif<span class="token operator">|</span>png<span class="token operator">|</span>bmp<span class="token operator">|</span>swf<span class="token operator">|</span><span class="token keyword">flv</span><span class="token punctuation">)</span>$<span class="token punctuation">{</span>
	<span class="token comment">#设置缓存时间为3600s（1h）</span>
	<span class="token keyword">add_header</span> Cache<span class="token operator">-</span>Control public<span class="token punctuation">,</span>max<span class="token operator">-</span>age<span class="token operator">=</span><span class="token number">3600</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>缓存的优点：</p> <ul><li>expires可以降低网站购买的带宽，节约成本；</li> <li>同时提升用户访问体验；</li> <li>减轻服务的压力，节约服务器成本，是web服务非常重要的功能。</li></ul> <p>缓存的缺点：</p> <ul><li>被缓存的页面或数据更新了，用户看到的可能还是旧的内容，反而影响用户体验。</li></ul> <p>解决办法：第一个缩短缓存时间，例如：1天，但不彻底，除非更新频率大于1天；第二个对缓存的对象改名。</p> <p>网站不希望被缓存的内容：</p> <ul><li>网站流量统计工具；</li> <li>更新频繁的文件（google的logo）。</li></ul> <h1 id="_3-代理"><a href="#_3-代理" class="header-anchor">#</a> 3.代理</h1> <h2 id="_3-1-什么是代理"><a href="#_3-1-什么是代理" class="header-anchor">#</a> 3.1 什么是代理</h2> <p>代理简单来说，就是如果我们想做什么，但又不想直接去做，那么这时候就找另外一个人帮我们去做。</p> <h2 id="_3-2-正向代理"><a href="#_3-2-正向代理" class="header-anchor">#</a> 3.2 正向代理</h2> <p>大家都知道，现在国内是访问不了 Google的，那么怎么才能访问 Google呢？我们又想，美国人不是能访问 Google吗（这不废话，Google就是美国的），如果我们电脑的对外公网 IP 地址能变成美国的 IP 地址，那不就可以访问 Google了。你很聪明，VPN 就是这样产生的。我们在访问 Google 时，先连上 VPN 服务器将我们的 IP 地址变成美国的 IP 地址，然后就可以顺利的访问了。</p> <h2 id="_3-3-反向代理"><a href="#_3-3-反向代理" class="header-anchor">#</a> 3.3 反向代理</h2> <p>反向代理和正向代理的区别就是：<strong>正向代理代理客户端，反向代理代理服务器。</strong></p> <p>反向代理，其实客户端对代理是无感知的，因为客户端不需要任何配置就可以访问，我们只需要将请求发送到反向代理服务器，由反向代理服务器去选择目标服务器获取数据后，在返回给客户端，此时反向代理服务器和目标服务器对外就是一个服务器，暴露的是代理服务器地址，隐藏了真实服务器IP地址。</p> <h2 id="_3-4-nginx配置反向代理"><a href="#_3-4-nginx配置反向代理" class="header-anchor">#</a> 3.4 nginx配置反向代理</h2> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token keyword">server</span> <span class="token punctuation">{</span>
    <span class="token keyword">listen</span>       <span class="token number">80</span><span class="token punctuation">;</span>
    <span class="token keyword">server_name</span>  www<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">;</span><span class="token comment"># 服务器地址或绑定域名</span>
 
    <span class="token keyword">location</span> <span class="token operator">^</span><span class="token operator">~</span> <span class="token operator">/</span>api <span class="token punctuation">{</span>  <span class="token comment"># ^~/api 表示匹配前缀为api的请求</span>
        <span class="token keyword">proxy_pass</span>  <span class="token keyword">http</span><span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>www<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">:</span><span class="token number">9527</span><span class="token operator">/</span>api<span class="token operator">/</span><span class="token punctuation">;</span>  <span class="token comment"># 注：proxy_pass的结尾有/， -&gt; 效果：会在请求时将/api/*后面的路径直接拼接到后面</span>
  
        <span class="token comment"># proxy_set_header作用：设置发送到后端服务器(上面proxy_pass)的请求头值  </span>
            <span class="token comment"># 【当Host设置为 $http_host 时，则不改变请求头的值;</span>
            <span class="token comment">#   当Host设置为 $proxy_host 时，则会重新设置请求头中的Host信息;</span>
            <span class="token comment">#   当为$host变量时，它的值在请求包含Host请求头时为Host字段的值，在请求未携带Host请求头时为虚拟主机的主域名;</span>
            <span class="token comment">#   当为$host:$proxy_port时，即携带端口发送 ex: $host:8080 】</span>
        <span class="token keyword">proxy_set_header</span> Host <span class="token variable">$host</span><span class="token punctuation">;</span> 
  
        <span class="token keyword">proxy_set_header</span> X<span class="token operator">-</span>Real<span class="token operator">-</span>IP <span class="token variable">$remote_addr</span><span class="token punctuation">;</span> <span class="token comment"># 在web服务器端获得用户的真实ip 需配置条件①    【 $remote_addr值 = 用户ip 】</span>
        <span class="token keyword">proxy_set_header</span> X<span class="token operator">-</span>Forwarded<span class="token operator">-</span>For <span class="token variable">$proxy_add_x_forwarded_for</span><span class="token punctuation">;</span><span class="token comment"># 在web服务器端获得用户的真实ip 需配置条件②</span>
        <span class="token keyword">proxy_set_header</span> REMOTE<span class="token operator">-</span>HOST <span class="token variable">$remote_addr</span><span class="token punctuation">;</span>
        <span class="token comment"># proxy_set_header X-Forwarded-For $http_x_forwarded_for; # $http_x_forwarded_for变量 = X-Forwarded-For变量</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h1 id="_4-负载均衡"><a href="#_4-负载均衡" class="header-anchor">#</a> 4.负载均衡</h1> <div class="language- line-numbers-mode"><pre class="language-text"><code>upstream exampleApi {
	server http://www.example1.com
	server http://www.example2.com
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></div> <footer class="page-edit" style="display:none;"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">2020-8-17 16:03:47</span></div></footer> <!----> <!----> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-40a3448a><li class="level-2" data-v-40a3448a><a href="/blogs/%E8%BF%90%E7%BB%B4/2020/nginx%E7%9B%B8%E5%85%B3.html#_2-1-工作进程数调优" class="sidebar-link" data-v-40a3448a>2.1 工作进程数调优</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E8%BF%90%E7%BB%B4/2020/nginx%E7%9B%B8%E5%85%B3.html#_2-2cpu亲和力调优" class="sidebar-link" data-v-40a3448a>2.2cpu亲和力调优</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E8%BF%90%E7%BB%B4/2020/nginx%E7%9B%B8%E5%85%B3.html#_2-3-最大打开文件数" class="sidebar-link" data-v-40a3448a>2.3 最大打开文件数</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E8%BF%90%E7%BB%B4/2020/nginx%E7%9B%B8%E5%85%B3.html#_2-4-事件处理模型调优" class="sidebar-link" data-v-40a3448a>2.4 事件处理模型调优</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E8%BF%90%E7%BB%B4/2020/nginx%E7%9B%B8%E5%85%B3.html#_2-5-开启高效传输模式" class="sidebar-link" data-v-40a3448a>2.5 开启高效传输模式</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E8%BF%90%E7%BB%B4/2020/nginx%E7%9B%B8%E5%85%B3.html#_2-6-连接超时时间" class="sidebar-link" data-v-40a3448a>2.6 连接超时时间</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E8%BF%90%E7%BB%B4/2020/nginx%E7%9B%B8%E5%85%B3.html#_2-7-fastcgi-调优" class="sidebar-link" data-v-40a3448a>2.7 fastcgi 调优</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E8%BF%90%E7%BB%B4/2020/nginx%E7%9B%B8%E5%85%B3.html#_2-8-压缩调优" class="sidebar-link" data-v-40a3448a>2.8 压缩调优</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E8%BF%90%E7%BB%B4/2020/nginx%E7%9B%B8%E5%85%B3.html#_2-8-1-gizp调优" class="sidebar-link" data-v-40a3448a>2.8.1 gizp调优</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E8%BF%90%E7%BB%B4/2020/nginx%E7%9B%B8%E5%85%B3.html#_2-8-2-brotli调优" class="sidebar-link" data-v-40a3448a>2.8.2 brotli调优</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E8%BF%90%E7%BB%B4/2020/nginx%E7%9B%B8%E5%85%B3.html#_2-9-缓存调优" class="sidebar-link" data-v-40a3448a>2.9 缓存调优</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E8%BF%90%E7%BB%B4/2020/nginx%E7%9B%B8%E5%85%B3.html#_2-9-1-expires" class="sidebar-link" data-v-40a3448a>2.9.1 expires</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E8%BF%90%E7%BB%B4/2020/nginx%E7%9B%B8%E5%85%B3.html#_2-9-2-cache-control" class="sidebar-link" data-v-40a3448a>2.9.2 cache-control</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E8%BF%90%E7%BB%B4/2020/nginx%E7%9B%B8%E5%85%B3.html#_3-1-什么是代理" class="sidebar-link" data-v-40a3448a>3.1 什么是代理</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E8%BF%90%E7%BB%B4/2020/nginx%E7%9B%B8%E5%85%B3.html#_3-2-正向代理" class="sidebar-link" data-v-40a3448a>3.2 正向代理</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E8%BF%90%E7%BB%B4/2020/nginx%E7%9B%B8%E5%85%B3.html#_3-3-反向代理" class="sidebar-link" data-v-40a3448a>3.3 反向代理</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E8%BF%90%E7%BB%B4/2020/nginx%E7%9B%B8%E5%85%B3.html#_3-4-nginx配置反向代理" class="sidebar-link" data-v-40a3448a>3.4 nginx配置反向代理</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-44bd5a18 data-v-44bd5a18><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-44bd5a18><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-44bd5a18></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-44bd5a18></path></svg></div></div></div>
    <script src="/assets/js/app.14e3b6f2.js" defer></script><script src="/assets/js/3.38ef4e4c.js" defer></script><script src="/assets/js/1.dd4aade8.js" defer></script><script src="/assets/js/32.e0da40e8.js" defer></script>
  </body>
</html>
